package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.CustomException;
import com.itheima.ydd.common.Result;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.SysMenuService;
import com.itheima.ydd.service.SysRoleMenuService;
import com.itheima.ydd.vo.SysMenuVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 菜单修改
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateById(sysMenu);
        return Result.ok("修改成功", null);
    }

    /**
     * 菜单删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public Result del(Long id) {
        //判断有无子级目录
        LambdaQueryWrapper<SysMenu> mlqw = new LambdaQueryWrapper<>();
        mlqw.eq(id != null, SysMenu::getPid, id);
        List<SysMenu> menus = sysMenuService.list(mlqw);
        if (menus != null) {
            throw new CustomException("该菜单下有子级目录不能删除");
        }

        //判断有无被用户使用
        LambdaQueryWrapper<SysRoleMenu> slqw = new LambdaQueryWrapper<>();
        slqw.eq(id != null, SysRoleMenu::getMenu_id, id);
        List<SysRoleMenu> roleMenus = sysRoleMenuService.list(slqw);
        if (roleMenus != null) {
            throw new CustomException("该菜单关联了用户,不能删除");
        }
        //可以删除
        sysMenuService.removeById(id);
        return Result.ok("删除成功", null);
    }


    /**
     * 所有菜单
     *
     * @return
     */
    @GetMapping("/allData")
    public Result allData() {
        LambdaQueryWrapper<SysMenu> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(SysMenu::getPid, 0).eq(SysMenu::getIs_show,1);
        List<SysMenu> list1 = sysMenuService.list(lqw1);

        //一级菜单
        List<SysMenuVo> sysMenuVos1 = list1.stream().map(item1 -> {
            SysMenuVo sysMenuVo1 = new SysMenuVo();
            BeanUtils.copyProperties(item1, sysMenuVo1);
            LambdaQueryWrapper<SysMenu> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(SysMenu::getPid, item1.getId());
            List<SysMenu> list2 = sysMenuService.list(lqw2);

            //二级菜单
            List<SysMenuVo> sysMenuVos2 = list2.stream().map(item2 -> {
                SysMenuVo sysMenuVo2 = new SysMenuVo();
                BeanUtils.copyProperties(item2, sysMenuVo2);
                LambdaQueryWrapper<SysMenu> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(SysMenu::getPid, item2.getId());
                List<SysMenu> list3 = sysMenuService.list(lqw3);
                sysMenuVo2.setChildren(list3);
                return sysMenuVo2;
            }).collect(Collectors.toList());

            sysMenuVo1.setChildren(sysMenuVos2);
            return sysMenuVo1;
        }).collect(Collectors.toList());

        return Result.ok("", sysMenuVos1);
    }


    /**
     * 菜单添加
     *
     * @param sysMenu
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody SysMenu sysMenu) {
        sysMenu.setCreate_date(LocalDateTime.now());
        sysMenuService.save(sysMenu);
        return Result.ok("添加成功", null);
    }

    /**
     * 菜单详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public Result rowInfo(Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        return Result.ok("", sysMenu);
    }
}
